package mylibrary.thread.waitAndNotify1;

public class NotifyTest {
	
	private String[] flag = {"true"};
	
	class NotifyThread extends Thread {
	
		public NotifyThread(String name) {
			super(name);
		}
		
		public void run() {
			try {
				sleep(3000);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			System.out.println(1111);
			
			synchronized (flag){
				flag[0] = "false";
				flag.notifyAll();
			}
		}
	}

	
	class WaitThread extends Thread {
		public WaitThread(String name) {
			super(name);
		}
		
		public void run() {
			synchronized (flag) {
				
				System.out.println("Enter");
				
				
				//if (flag[0] != "false") {
				while (flag[0] != "false") {	
					System.out.println(getName() + " begin waiting!");
					long waitTime = System.currentTimeMillis();
					try {
						flag.wait();
					} catch (Exception e) {
						e.printStackTrace();
					}

					waitTime = System.currentTimeMillis() - waitTime;
					System.out.println("wait time : " + waitTime);
				}
				System.out.println(getName() + " end waiting!");
				
			}
		}
	}
	
	public static void main(String[] args) {
		System.out.println("Main thread run!");
		
		NotifyTest test = new NotifyTest();
		
		NotifyThread notifyThread = test.new NotifyThread("notify01");
		WaitThread waitThread01 = test.new WaitThread("wait01");
		WaitThread waitThread02 = test.new WaitThread("wait02");
		WaitThread waitThread03 = test.new WaitThread("wait03");
		
		notifyThread.start();
		waitThread01.start();
		waitThread02.start();
		waitThread03.start();
	}
}
